package com.jader.fragment;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jader.activities.Store;
import com.jader.adapter.CollectAdapter;
import com.jader.beans.CollectStore;
import com.jader.util.JaderApplication;
import com.jader.util.PreferenceUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class CollectStoreFragment extends Fragment {
	List<CollectStore> sellers_list = new ArrayList<CollectStore>();
	CollectAdapter storeAdapter;
	ListView listView;
	ImageView img_more, img_delete;
	View view;
	private HttpUtils httpUtils;
	String url = JaderApplication.getIP() + "/Jader/client";

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// storeAdapter.notifyDataSetChanged();
		sendRequest();
	}

	private void sendRequest() {
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", ""
				+ JaderApplication.COLLECT_STORE_SELECT);
		params.addBodyParameter("userid",
				PreferenceUtil.getInstance(getActivity()).getShareUserId());
		httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						JaderApplication.show(
								CollectStoreFragment.this.getActivity(), "请求超时");

					}

					@Override
					public void onSuccess(ResponseInfo<String> info) {
						String result = info.result;
						Gson gson = new Gson();
						Type typeofT = new TypeToken<List<CollectStore>>() {
						}.getType();
						List<CollectStore> list = gson
								.fromJson(result, typeofT);

						for (CollectStore goods : list) {
							boolean flag = true;
							for (CollectStore goods2 : sellers_list) {
								if (goods.equals(goods2)) {
									flag = false;
								}
							}
							if (flag) {
								sellers_list.addAll(list);
							}
						}
						storeAdapter.notifyDataSetChanged();
					}
				});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_store_customm, null);
		initView();
		return view;
	}

	private void initView() {
		img_more = (ImageView) view.findViewById(R.id.img_collect_more);
		listView = (ListView) view.findViewById(R.id.listview_store_custom);
		storeAdapter = new CollectAdapter(sellers_list, getActivity());
		listView.setAdapter(storeAdapter);
		viewListener();
	}

	// 事件监听
	private void viewListener() {
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				int sellerid = sellers_list.get(position).getSeller()
						.getSellerid();
				Intent store = new Intent(getActivity(), Store.class);
				store.putExtra("sellerid", sellerid);
				startActivity(store);
			}
		});
	}
}
